/*
* @Author: yongqiang.sun
* @Date:   2018-01-12 21:33:37
* @Last Modified by:   yongqiang.sun
* @Last Modified time: 2018-01-13 18:40:32
*/
(function (window) {
    var $ = window.Zepto || window.jQuery;
    function renderOrderItem (data) {
        var orderItem = $('#orderItem').html();
        var coItem = $('#orderBody').html();
        var html = [];
        data.list.forEach(function (item) {
            var foods = []
            item['orderStatusText'] = item.orderStatus == 1 ? '已完成' : '待取货';
            item.itemList.forEach(function (co) {
                co['statusText'] = co.status == 1 ? '已出货' : '';
                foods.push(Mustache.render(coItem, co));
            });
            item.orderBody = foods.join('');
            html.push(Mustache.render(orderItem, item));
        })
        $('.order').append(html.join(''));
    }
    $(window.document).ready(function () {
        $('.order-loader').dropload({
            scrollArea: $('.user-order'),
            distance: 80,
            loadDownFn : function(me) {
                $.ajax({
                    type: 'GET',
                    url: 'json/more.json',
                    dataType: 'json',
                    success: function(data) {
                        console.log(data)
                        // 每次数据加载完，必须重置
                        me.resetload();
                    },
                    error: function(xhr, type) {
                        // 即使加载出错，也得重置
                        var data = {
                            list: [{
                                id: '1',
                                orderStatus: 0,
                                no: '1121323123',
                                itemList: [{
                                    goodsId: '1',
                                    coverSmallImg: '/asset/images/coco/brand.png',
                                    goodsName: '摩卡咖啡',
                                    goodsPrice: 30,
                                    goodsQuantity: 2,
                                    status: 0
                                }, {
                                    goodsId: '2',
                                    coverSmallImg: '/asset/images/coco/brand.png',
                                    goodsName: '摩卡咖啡',
                                    goodsPrice: 30,
                                    goodsQuantity: 2,
                                    status: 1
                                }]
                            }, {
                                id: '2',
                                orderStatus: 2,
                                no: '1121323123',
                                itemList: [{
                                    goodsId: '1',
                                    coverSmallImg: '/asset/images/coco/brand.png',
                                    goodsName: '摩卡咖啡',
                                    goodsPrice: 30,
                                    goodsQuantity: 2,
                                    status: 0
                                }]
                            }]
                        }
                        renderOrderItem(data);
                        me.noData(false);
                        me.resetload();
                    }
                });
            }
        }).unlock();
    })
})(window);
